package com.obao.user.action;

import com.obao.business.entity.Product;
import com.obao.business.entity.ProductFlavor;
import com.obao.business.entity.ProductSize;
import com.obao.business.service.IProductService;
import com.obao.user.entity.BusinessItem;
import com.obao.user.entity.Cart;
import com.obao.user.entity.ProductItem;
import com.obao.user.entity.User;
import com.obao.user.service.ICartService;
import com.obao.user.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.util.*;

public class CartAction extends ActionSupport {
    private ICartService cartService;
    private IUserService userService;
    private IProductService productService;
    private Product product = null;
    private Cart userCart;
    private Integer number;
    private boolean flag = false;
    private ProductSize productSize = null;
    private ProductFlavor productFlavor = null;
    private List<BusinessItem> businessItems = null;
    private User user;

    /**
     * 查找我的购物车
     *
     * @return
     */
    public String findMyCart() {
        try {

            userCart = cartService.findCart(user);

            return "myCart";
        } catch (Exception e) {
            e.printStackTrace();
            return "flag";
        }
    }

    /**
     * 添加购物车1
     * @return
     */
    public String add() {
        user = new User();
        //购买用户
        user.setUserId(2);
        //购买数量
        int number = Integer.parseInt(ServletActionContext.getRequest().getParameter("number"));
        //调用购物车业务
        cartService.save(user,number,product,productFlavor,productSize);
        return null;
    }
    /**
     * 删除购物车
     *
     * @return
     */
    public String delete(){
        Integer productItemId = Integer.parseInt(ServletActionContext.getRequest().getParameter("productItemId"));
         cartService.delete(productItemId);
        return null;
    }
    public String findBuyingBill(){
        try{
            String productItemIds = ServletActionContext.getRequest().getParameter("productItemIds");
            String businessItemIds = ServletActionContext.getRequest().getParameter("businessItemIds");
            businessItems = cartService.findBuyingBill(businessItemIds,productItemIds);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "buyRS";
    }

    /**
     * get and set....
     */

    public void setCartService(ICartService cartService) {
        this.cartService = cartService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public Cart getUserCart() {
        return userCart;
    }

    public void setUserCart(Cart userCart) {
        this.userCart = userCart;
    }

    public ProductFlavor getProductFlavor() {
        return productFlavor;
    }

    public void setProductFlavor(ProductFlavor productFlavor) {
        this.productFlavor = productFlavor;
    }

    public ProductSize getProductSize() {
        return productSize;
    }

    public void setProductSize(ProductSize productSize) {
        this.productSize = productSize;
    }

    public void setProductService(IProductService productService) {

        this.productService = productService;
    }

    public List<BusinessItem> getBusinessItems() {
        return businessItems;
    }

    public void setBusinessItems(List<BusinessItem> businessItems) {
        this.businessItems = businessItems;
    }
}
